package com.musicmall.service.Impl;

import com.musicmall.common.Const;
import com.musicmall.common.ServerResponse;
import com.musicmall.dao.FavoriteMapper;
import com.musicmall.dao.SinglesMapper;
import com.musicmall.pojo.Favorite;
import com.musicmall.pojo.Singles;
import com.musicmall.service.ISinglesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

@Service("iSinglesService")
public class SinglesServiceImpl implements ISinglesService {
    @Autowired
    private SinglesMapper singlesMapper;

    @Autowired
    private FavoriteMapper favoriteMapper;

    public ServerResponse getSinglesList(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Singles> singlesList = singlesMapper.selectList();
        PageInfo pageResult = new PageInfo(singlesList);
        return ServerResponse.createBySuccess(pageResult);

    }

    public ServerResponse searchSingles(String keyword){
        String searchKey = new StringBuilder().append("%").append(keyword).append("%").toString();
        List<Singles> singlesList = singlesMapper.selectByNameOrArtistOrAlbum(searchKey);
        return ServerResponse.createBySuccess(singlesList);
    }

    public ServerResponse addFav(Integer userId,Integer singlesId){
        Singles singles = singlesMapper.selectByPrimaryKey(singlesId);
        if(singles ==null){
            return ServerResponse.createByErrorMessage("没有找到该单曲id！");
        }
        int singleCount = favoriteMapper.checkBySingleId(singlesId);
        if(singleCount>0){
            return ServerResponse.createByErrorMessage("该单曲已加入过收藏夹了！");
        }
        Favorite favorite = new Favorite();
        favorite.setUserId(userId);
        favorite.setSinglesId(singlesId);
        favorite.setArtistId(singles.getArtistId());
        favorite.setAlbumId(singles.getAlbumId());
        favorite.setSinglesName(singles.getName());
        favorite.setArtistName(singles.getArtistName());
        favorite.setAlbumName(singles.getAlbumName());
        favorite.setItemType(Const.ItemType.SINGLES);
        int count = favoriteMapper.insert(favorite);
        if(count==1){
            return ServerResponse.createBySuccess("添加到收藏夹成功");
        }
        else{
            return ServerResponse.createByErrorMessage("添加到收藏夹失败");
        }
    }

    public ServerResponse delFav(Integer userId,Integer singlesId){
        Singles singles = singlesMapper.selectByPrimaryKey(singlesId);
        if(singles ==null){
            return ServerResponse.createByErrorMessage("没有找到该单曲id！");
        }

        int singleCount = favoriteMapper.checkBySingleId(singlesId);
        if(singleCount==0){
            return ServerResponse.createByErrorMessage("收藏夹中没有该单曲！");
        }
        int rowCount = favoriteMapper.deleteByPrimaryKey(singlesId);
        if(rowCount==1){
            return ServerResponse.createBySuccess("单曲从收藏夹中移除成功！");
        }
        return ServerResponse.createByErrorMessage("移除失败!");




    }



}
